package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;

import com.goods.business.mapper.UserMappers;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.response.ActiveUser;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.goods.common.vo.system.UserInfoVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goods.business.service.HealthService;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;


@Service
public class HealthServiceImpl implements HealthService {


    @Autowired
    private HealthMapper healthMapper;



    /**
     * 健康打卡提醒
     * @return
     */
    @Override
    public Health isReport() {
        //查询User对象的信息
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user = activeUser.getUser();

        Health health = healthMapper.isReport(user.getId(),new Date());
        if (health != null){
            return health;
        }
        return null;
    }


    /**
     * 打卡
     * @param healthVO
     * @return
     */
    @Override
    public void report(HealthVO healthVO) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        //封装对象
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        //userId
        health.setUserId(activeUser.getUser().getId());
        //时间
        health.setCreateTime(new Date());

        healthMapper.insert(health);
    }

    /**
     * 签到记录
     * @return
     */
    @Override
    public PageVO<Health> history(int pageSize, int pageNum) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        //封装条件查询对象
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",activeUser.getUser().getId());
        List<Health> healthList = healthMapper.selectByExample(example);
        //判断
        if (healthList != null){
            // 构建分页对象
            PageHelper.startPage(pageNum,pageSize);
            PageInfo<Health> pageInfo = new PageInfo<>(healthList);
            return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
        }
        return null;
    }
}
